package com.luoxiang.socialandroid.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.app.MyAppliaction;
import com.luoxiang.socialandroid.bean.Host;

import java.util.List;

public class YouxuanAdapter extends RecyclerView.Adapter<YouxuanAdapter.ViewHolder> {
    private List<Host> list;

    public YouxuanAdapter(List<Host> list) {
        this.list = list;
    }

    public static interface OnAddClickListener {
        // true add; false cancel
        void onItemClicks(int position, int userid,String userName,String icon); //传递boolean类型数据给activity
    }

    YouxuanAdapter.OnAddClickListener onItemAddClick;

    public void setOnAddClickListener(YouxuanAdapter.OnAddClickListener onItemAddClick) {
        this.onItemAddClick = onItemAddClick;
    }

    private String url = "https://dahuixiong.oss-cn-shenzhen.aliyuncs.com/";

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_tuijian, parent, false);
        YouxuanAdapter.ViewHolder holder = new YouxuanAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView")int position) {
        Host bean = list.get(position);
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.icon_error_img2)//图片加载出来前，显示的图片
                .fallback(R.mipmap.icon_error_img2) //url为空的时候,显示的图片
                .error(R.mipmap.icon_error_img2);//图片加载失败后，显示的图片
        RoundedCorners roundedCorners = new RoundedCorners(30);
        RequestOptions options1 = RequestOptions.bitmapTransform(roundedCorners);
        if (bean.getOnline() == 2) {
            holder.im1_tuijian.setImageResource(R.mipmap.icon_zailiao);
            holder.im1_tuijian.setVisibility(View.VISIBLE);
        } else if (bean.getOnline() == 3) {
            holder.im1_tuijian.setImageResource(R.mipmap.icon_online);
            holder.im1_tuijian.setVisibility(View.VISIBLE);
        } else {
            holder.im1_tuijian.setVisibility(View.GONE);
        }
        holder.icon_txname_bg.bringToFront();
        holder.tx2_tuijian.setText(bean.getName());
        if (bean.getCoverType() == 0) {
            //设置图片
            Glide.with(MyAppliaction.getContext())
                    .load(url + bean.getCover())
                    .transform(new CenterCrop(),new RoundedCorners(30))
                    .into(holder.im_tuijian);
            holder.img_isshipin.setVisibility(View.GONE);
        } else {
            Glide.with(MyAppliaction.getContext())
                    .load(url + bean.getCover() + "?x-oss-process=video/snapshot,t_1,f_png,w_0,h_0,ar_auto")
                    .transform(new CenterCrop(),new RoundedCorners(30))
                    .into(holder.im_tuijian);
            holder.img_isshipin.setVisibility(View.VISIBLE);
//            LogUtils.i("shipin",url + hostBean.getCover() + "x-oss-process=video/snapshot,t_1,f_png,w_0,h_0,ar_auto");
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemAddClick.onItemClicks(position,bean.getUserId(),bean.getName(),bean.getIcon());
            }
        });
    }

    @Override
    public int getItemCount() {
        //防空判断
        try {
            return list.size();
        } catch (Exception e) {
            return 0;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView im_tuijian, img_isshipin, im1_tuijian,icon_txname_bg;
        private TextView tx2_tuijian;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            icon_txname_bg = itemView.findViewById(R.id.icon_txname_bg);
            im_tuijian = itemView.findViewById(R.id.im_tuijian);
            im1_tuijian = itemView.findViewById(R.id.im1_tuijian);
            tx2_tuijian = itemView.findViewById(R.id.tx2_tuijian);
            img_isshipin = itemView.findViewById(R.id.img_isshipin);
        }
    }
}
